﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

namespace MailboxRightsLookup
{
    class MailboxDetails
    { 
        Guid _guid;

        string _displayName;
        string _sAMAccountName;
        IEnumerable<string> _proxyAddresses;
        string _homeMDB;
        string _distinguishedName;

        public string guid { get { return _guid.ToString(); } }
        public string displayName { get { return _displayName; } }
        public string sAMAccountName { get { return _sAMAccountName; } }
        public string primarySmtp
        {
            get { return _proxyAddresses.Where((str) => str.Contains("SMTP")).First().Substring(5); }
        }
        public string database
        {
            get { return _homeMDB.Substring(3, _homeMDB.IndexOf(',') - 3); }
        }
        public string distinguishedName { get { return _distinguishedName; } }

        public MailboxDetails(Guid guid, string path)
        {
            _guid = guid;
            DomainUtils du = DomainUtils.Instance;
            UserPrincipal user = UserPrincipal.FindByIdentity(
                du.GetPrincipalContext(path, DomainUtils.DomainNameType.Path),
                IdentityType.Guid, 
                guid.ToString());
            _displayName = user.DisplayName;
            _sAMAccountName = user.SamAccountName;
            _distinguishedName = user.DistinguishedName;

            DirectoryEntry de = user.GetUnderlyingObject() as DirectoryEntry;
            _proxyAddresses = de.Properties["proxyAddresses"].Cast<string>();
            _homeMDB = (string)de.Properties["homeMDB"][0];
        }
    }


}
